package pl.jdeveloper.derivato.web.form;

import java.io.IOException;
import java.util.Scanner;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.lang.Bytes;

public class FileUploadForm extends Form{

	private static final long serialVersionUID = -2145336517841735018L;

	private FileUploadField fileInput;

	public FileUploadForm(String id, IModel model) {
		super(id, model);
		// TODO Auto-generated constructor stub
	}
	
	public FileUploadForm(String id) {
		super(id);
		setMultiPart(true);
		fileInput = new FileUploadField("fileInput");
		setMaxSize(Bytes.kilobytes(200));
		add(fileInput);
	}

	@Override
	protected void onSubmit() {
		final FileUpload upload = fileInput.getFileUpload();
		if (upload != null){
			try {
				Scanner scanner = new Scanner(upload.getInputStream());
				scanner.useDelimiter(";");
				while (scanner.hasNextLine()){
					System.out.println("kolejna linia");
					info("kolejna linia");
				}
			} catch (IOException e) {
				this.error("dupa z wczytania pliku");
				e.printStackTrace();
			}
		}
		 
	}
	
	
}
